package cloud.misiro.spring.cloud.open.feign.support;

import feign.Contract;
import feign.MethodMetadata;
import feign.Request;
import org.springframework.web.bind.annotation.PostMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class SpringMvcContract extends Contract.BaseContract {
    @Override
    protected void processAnnotationOnClass(MethodMetadata methodMetadata, Class<?> aClass) {

    }

    @Override
    protected void processAnnotationOnMethod(MethodMetadata methodMetadata, Annotation annotation, Method method) {
        if (annotation instanceof PostMapping) {
            PostMapping postMapping = (PostMapping) annotation;
            methodMetadata.template().method(Request.HttpMethod.POST);
            String path = postMapping.value()[0];
            if (!path.startsWith("/") && !methodMetadata.template().path().endsWith("/")) {
                path = "/" + path;
            }
            methodMetadata.template().uri(path, true);
        }
    }

    @Override
    protected boolean processAnnotationsOnParameter(MethodMetadata methodMetadata, Annotation[] annotations, int i) {
        return true;
    }
}
